package com.f5.management.service.impl;

import com.f5.management.dao.CommunityDAO;
import com.f5.management.dao.ParkingDAO;
import com.f5.management.dao.ParkingManagementDAO;
import com.f5.management.entity.Community;
import com.f5.management.entity.Parking;
import com.f5.management.entity.ParkingManagement;
import com.f5.management.service.ParkingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class ParkingServiceImpl implements ParkingService {
    @Autowired
    ParkingDAO parkingDAO;
    @Autowired
    CommunityDAO communityDAO;
    @Autowired
    ParkingManagementDAO pmDAO;

    @Override
    // 事务传播，获取与当前事务一致的环境
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Parking> list() {
        return parkingDAO.list();
    }

    @Override
    public Parking get(int id) {
        return parkingDAO.get(id);
    }

    @Override
    public void insert(Parking parking) {
        Community community = communityDAO.getByName(parking.getCommunity().getName());
        parking.setCommunity(community);
        // 车位编号规则：时间yyyy_MM_dd + uuid前两位
        String uuid = UUID.randomUUID().toString().substring(0, 5);
        Date dNow = new Date( );
        SimpleDateFormat ft = new SimpleDateFormat ("hh_mm_ss");
        String format = ft.format(dNow);

        parking.setSerialNumber(format+uuid);
        parkingDAO.insert(parking);

        // 将车位纳入车位管理
        ParkingManagement parkingManagement = new ParkingManagement();

        parkingManagement.setCost(0);
        parkingManagement.setStartTime(null);
        parkingManagement.setEndTime(null);
        parkingManagement.setParking(parking);
        parkingManagement.setStatus(-1);
        parkingManagement.setVehicle(null);

        pmDAO.insert(parkingManagement);
    }

    @Override
    public void update(Parking parking) {
        int id = parking.getId();
        Parking p = parkingDAO.get(id);

        int cid = p.getCommunity().getId();

        String cname = parking.getCommunity().getName();

        Date createTime = p.getCreateTime();

        Community community = p.getCommunity();

        parking.setCreateTime(createTime);

        community.setName(cname);

        community.setId(cid);
        parking.setCommunity(community);

        parkingDAO.update(parking);
    }

    @Override
    public void delete(int id) {
        Parking parking = parkingDAO.get(id);
        ParkingManagement pm = pmDAO.getByParking(parking);

        int pid = pm.getId();

        parkingDAO.delete(pid);
    }

    @Override
    public Long count() {
        return parkingDAO.count();
    }

    @Override
    public List<Parking> findByPage(Integer pageNow, Integer rows) {
        int start = (pageNow - 1) * rows;
        return parkingDAO.findByPage(start, rows);
    }
}
